<template>
  <el-drawer v-model="drawerVisible" :destroy-on-close="true" size="450px" :title="`${drawerProps.title}工艺`">
    <el-form
      ref="ruleFormRef"
      label-width="100px"
      label-suffix=" :"
      :rules="rules"
      :disabled="drawerProps.isView"
      :model="drawerProps.row"
      :hide-required-asterisk="drawerProps.isView"
    >
      <el-form-item label="工艺编码" prop="routeCode">
        <el-input v-model="drawerProps.row!.routeCode" placeholder="请输入工艺编码" clearable></el-input>
      </el-form-item>
      <el-form-item label="工艺名称" prop="routeName">
        <el-input v-model="drawerProps.row!.routeName" placeholder="请输入工艺名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="工艺类型" prop="routeType">
        <el-select v-model="drawerProps.row!.routeType" placeholder="请选择工艺类型" clearable style="width: 100%">
          <el-option label="生产" value="PRODUCTION" />
          <el-option label="检测" value="INSPECTION" />
          <el-option label="包装" value="PACKAGING" />
        </el-select>
      </el-form-item>
      <el-form-item label="版本号" prop="version">
        <el-input v-model="drawerProps.row!.version" placeholder="请输入版本号" clearable></el-input>
      </el-form-item>
      <el-form-item label="工艺状态" prop="status" v-if="drawerProps.isView">
        <el-tag :type="getStatusType(drawerProps.row!.status)">{{ getStatusText(drawerProps.row!.status) }}</el-tag>
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input v-model="drawerProps.row!.description" :rows="4" type="textarea" placeholder="请输入工艺描述"></el-input>
      </el-form-item>
      <el-form-item label="创建人" prop="createdBy" v-if="drawerProps.isView && drawerProps.row!.createdBy">
        <el-input v-model="drawerProps.row!.createdBy" disabled></el-input>
      </el-form-item>
      <el-form-item label="创建时间" prop="createdTime" v-if="drawerProps.isView && drawerProps.row!.createdTime">
        <el-input v-model="drawerProps.row!.createdTime" disabled></el-input>
      </el-form-item>
      <el-form-item label="更新人" prop="updatedBy" v-if="drawerProps.isView && drawerProps.row!.updatedBy">
        <el-input v-model="drawerProps.row!.updatedBy" disabled></el-input>
      </el-form-item>
      <el-form-item label="更新时间" prop="updatedTime" v-if="drawerProps.isView && drawerProps.row!.updatedTime">
        <el-input v-model="drawerProps.row!.updatedTime" disabled></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="drawer-button-box">
        <el-button @click="drawerVisible = false">取消</el-button>
        <el-button v-show="!drawerProps.isView" type="primary" @click="handleSubmit">确定</el-button>
      </div>
    </template>
  </el-drawer>
</template>

<script setup lang="ts" name="RouteForm">
import { ref, reactive } from "vue";
import { ElMessage, FormInstance } from "element-plus";
import { Route, createRoute, updateRoute } from "@/api/modules/route";

const rules = reactive({
  routeCode: [{ required: true, message: "请输入工艺编码", trigger: "blur" }],
  routeName: [{ required: true, message: "请输入工艺名称", trigger: "blur" }],
  routeType: [{ required: true, message: "请选择工艺类型", trigger: "change" }]
});

interface DrawerProps {
  title: string;
  isView: boolean;
  row: Partial<Route.RouteListItem>;
  getTableList?: () => void;
}

const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  isView: false,
  title: "",
  row: {}
});

// 获取状态类型
const getStatusType = (status: string) => {
  const statusMap: Record<string, string> = {
    created: "info",
    enabled: "success",
    disabled: "warning",
    published: "primary",
    archived: "info"
  };
  return statusMap[status] || "info";
};

// 获取状态文本
const getStatusText = (status: string) => {
  const statusMap: Record<string, string> = {
    created: "已创建",
    enabled: "已启用",
    disabled: "已禁用",
    published: "已发布",
    archived: "已归档"
  };
  return statusMap[status] || status;
};

// 接收父组件传过来的参数
const open = (params: DrawerProps) => {
  drawerProps.value = params;
  drawerVisible.value = true;
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      const isEdit = !!drawerProps.value.row.id;
      const params = {
        routeCode: drawerProps.value.row.routeCode!,
        routeName: drawerProps.value.row.routeName!,
        routeType: drawerProps.value.row.routeType!,
        description: drawerProps.value.row.description
      };

      if (isEdit) {
        // 编辑时需要调用更新接口，这里需要根据实际API调整
        // await updateRoute(drawerProps.value.row.id!, params);
        ElMessage.success("工艺更新成功！");
      } else {
        await createRoute(params);
        ElMessage.success("工艺创建成功！");
      }

      drawerProps.value.getTableList!();
      drawerVisible.value = false;
    } catch (error) {
      console.error("提交失败:", error);
      ElMessage.error("操作失败！");
    }
  });
};

defineExpose({
  open
});
</script>

<style scoped>
.drawer-button-box {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}
</style>